/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * seleccionarMedico.java
 *
 * Created on 04-may-2011, 22:06:41
 */

package interfaces;

import GestorCitas.Cita;
import GestorUsuarios.Medico;
import GestorUsuarios.UsuarioDAO;
import clinica.ClinicaView;
import clinica.Interfaces;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import javax.swing.DefaultListModel;
import org.jdesktop.application.Action;
import Controlador.*;
import GestorUsuarios.Usuario;
import clinica.Interfaz;

/**
 *
 * @author Victor
 */
public class seleccionarMedico extends javax.swing.JPanel {

    private ClinicaView app;
    private Cita cita;
    private Medico [] medicos;

    /** Creates new form seleccionarMedico */
    public seleccionarMedico(ClinicaView a) {
        initComponents();
        app = a;
        lista.setFixedCellHeight(32);
    }

    public void rellenarDatos(Cita c) throws SQLException, IOException, ClassNotFoundException, NoSuchAlgorithmException {
        cita = c;
        medico.setText("Médico actual: "+Usuario.get(c.getIdMedico()).getNombre());
        UsuariosDisponibles[] meds = UsuarioDAO.getMedicosDisponiblesOptimizado(c.getFechaCita(), c.getFechaFinalizacion());
        if(meds == null) {
            medicos = null;
        } else {
            medicos = new Medico[meds[0].getArrayId().size()];
            for(int i=0;i<meds[0].getArrayId().size();i++)
                medicos[i] = (Medico) Usuario.get(meds[i].getArrayId().get(i));

        }
        if(medicos!=null) {
            DefaultListModel modelo = new DefaultListModel();
            lista.setModel(modelo);
            for(Medico m:medicos) {
                modelo.addElement("Dr." + m.getNombre() + " " + m.getApellidos());
            }

        } else {
            app.abrirMensajeError("No se encontraron médicos para la cita.", Interfaces.CONSULTAR_CITA);
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        medico = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lista = new javax.swing.JList();

        setName("Form"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(seleccionarMedico.class, this);
        jButton1.setAction(actionMap.get("aceptar")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(seleccionarMedico.class);
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("cancelar")); // NOI18N
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel1.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize())); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        medico.setFont(Interfaz.setFontSize());
        medico.setText(resourceMap.getString("medico.text")); // NOI18N
        medico.setName("medico"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        lista.setFont(Interfaz.setFontSize());
        lista.setName("lista"); // NOI18N
        jScrollPane1.setViewportView(lista);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 728, Short.MAX_VALUE)
                    .addComponent(medico, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 728, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(medico)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void cancelar() {
        app.volver(Interfaces.CONSULTAR_CITA);
    }

    @Action
    public void aceptar() throws SQLException, IOException, ClassNotFoundException {
        if(lista.getSelectedIndex()>=0) {
            cita.setIdMedico(lista.getSelectedIndex());
            Cita.update(cita);
            app.abrirMensajeExito("Médico cambiado correctamente", Interfaces.CONSULTAR_CITA);
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lista;
    private javax.swing.JLabel medico;
    // End of variables declaration//GEN-END:variables

}
